﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Vcare.Models;
using Vcare.DAL;
using Vcare.ViewModels;
using System.Web.Security;


namespace Vcare.Controllers
{
    public class PlusController : Controller
    {
        private VcareContext db = new VcareContext();

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult PurchaseCard()
        {
          ViewModelMembershipCards ViewModel = new ViewModelMembershipCards();

          return View(ViewModel);
        }

        [HttpPost]
        public ActionResult PurchaseCard(ViewModelMembershipCards ViewModel)
        {
          Guid UserID = (Guid)Membership.GetUser().ProviderUserKey;

          for (int i = 0; i < ViewModel.NumberOfCards; i++)
          {
            PurchasedMembers pm = new PurchasedMembers();
            pm.DateCreated = ViewModel.PurchasedMember.DateCreated;
            pm.Price = ViewModel.PurchasedMember.Price;
            pm.SoldOnPrice = ViewModel.PurchasedMember.SoldOnPrice;
            pm.UserID = UserID;
            db.PurchasedMembers.Add(pm);
          }
          
          db.SaveChanges();
          return RedirectToAction("Index", "Home");
        }

        public ActionResult ListOfPurchasedMembers()
        {
          ViewBag.NumAvailableMembers = db.PurchasedMembers.Count() - db.AllocatedMembers.Count();

          ViewModelMembershipCards ViewModel = new ViewModelMembershipCards();
          ViewModel.ListPurchasedMembers = db.PurchasedMembers
            .Include(m => m.User)
            .ToList();
                     

          return View(ViewModel);
        }

        public ActionResult Allocate(int CompanyID)
        {
          ViewBag.NumAvailableMembers = db.PurchasedMembers.Count() - db.AllocatedMembers.Count();
          ViewModelMembershipCards ViewModel = new ViewModelMembershipCards();
          ViewModel.CompanyID = CompanyID;
          ViewBag.CompanyName = db.Companies.Find(CompanyID).Company;

          return View(ViewModel);
        }

        [HttpPost]
        public ActionResult Allocate(ViewModelMembershipCards ViewModel)
        {
          Guid UserID = (Guid)Membership.GetUser().ProviderUserKey;
          string sme = db.Companies.Find(ViewModel.CompanyID).Company;
          for (int i = 0; i < ViewModel.NumberOfCards; i++)
          {
            PurchasedMembers purchaseMembers = (from p in db.PurchasedMembers
                                     where !db.AllocatedMembers.Any(a => a.PurchasedMembersID == p.PurchasedMembersID)
                                     select p)
                                    .OrderBy(p => p.PurchasedMembersID)
                                    .First();
            // determine the next index number
            string todaysDate = DateTime.Now.ToString("ddMMyy");
            int nextIndex = 1;
            if (db.Members.Where(m => m.MembershipID.Contains(todaysDate)).Count() != 0)
            {
              string memberIndex = db.Members
                          .Where(m => m.MembershipID.Contains(todaysDate))
                          .OrderByDescending(m => m.MembershipID)
                          .FirstOrDefault()
                          .MembershipID;
              nextIndex = Convert.ToInt16(memberIndex.Substring(7)) + 1;
            }
            Members newMember = new Members();
            newMember.MembershipID = "9" + DateTime.Now.ToString("ddMMyy") + nextIndex.ToString().PadLeft(3, '0');
            newMember.CompanyID = ViewModel.CompanyID;
            newMember.Firstname = string.Format("{0} member", sme);
            newMember.ContactDate = DateTime.Now.AddHours(8);
            newMember.ValueOfSinglePayment = purchaseMembers.SoldOnPrice;
            int? callCentreid = db.CallCentres.Where(m => m.CompanyID == ViewModel.CompanyID).FirstOrDefault().CallCentreID;
            newMember.CallCentreID = callCentreid;
            newMember.Paid = true;
            newMember.QAd = true;
            db.Members.Add(newMember);
            AllocatedMembers am = new AllocatedMembers();
            am.Member = newMember;
            am.CompanyID = ViewModel.CompanyID;
            am.PurchasedMembersID = purchaseMembers.PurchasedMembersID;
            db.AllocatedMembers.Add(am);
            db.SaveChanges();
          }
          return RedirectToAction("ListOfPurchasedMembers");
        }
    }
}
